<template>
  <div>
    <el-dialog :visible.sync="dialogFormVisible" title="管理机柜" width="620px">
      <div class="grid-container">
        <el-button
          class="grid-item"
          v-for="item in list"
          :key="item.position"
          :class="{
            active: item.isSelected,
            outside: item.device?.is_locked === true,
            inside: item.device?.is_locked === false
          }"
          @click="toggleSelection(item)"
          :disabled="item.disabled"
        >
          {{ item.position }}
          <div
            v-if="item.device"
            :class="[
              'upgrade_status',
              item.device?.upgrade_status === 0 ? 'upgrade_status_0' : 'upgrade_status_1'
            ]"
          >
            <p>{{ item.device?.upgrade_status === 0 ? 0 : 1 }}</p>
          </div>
          <div class="upgrade_progress" v-if="item.device && item.device?.upgrade_status === 0">
            <p>{{ item.device?.upgrade_progress ?? 0 }}%</p>
          </div>
        </el-button>
      </div>
      <el-divider></el-divider>
      <div class="explain">
        <p class="inside">未锁定</p>
        <p class="outside">已锁定</p>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button
          v-if="firstClickStatus === false && this.device_numbers.length > 0"
          type="danger"
          @click="submit('lock')"
          >锁定</el-button
        >
        <el-button
          v-if="firstClickStatus === true && this.device_numbers.length > 0"
          type="primary"
          @click="submit('unlock')"
          >解锁</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCabinetsList, deviceLock } from '@/api/device'
export default {
  name: 'SetDevice',
  data() {
    return {
      cabinet_id: '', // 机柜Id
      dialogFormVisible: false,
      checkList: [],
      device_numbers: [], // 选中的讲解器列表
      clickStatus: {}, // 用于存储每个 item 的点击状态
      firstClickStatus: null, // 第一个选择的状态
      // 示例数据，根据你提供的字段结构
      list: []
    }
  },
  methods: {
    handleDeviceList(id) {
      this.cabinet_id = id
      getCabinetsList(id).then((res) => {
        console.log(res)
        this.dialogFormVisible = true
        this.list = res.data.list_data.map((item) => {
          return {
            ...item,
            isSelected: false,
            disabled: !item.device
          }
        })
      })
    },

    // 判断是否禁用格子
    isDisabled(item) {
      if (item.device === null) {
        return true
      } else {
        // 如果没有选择第一个，则所有格子都可以选择
        if (this.firstSelectedStatus === null) {
          return false
        }
        // 如果第一个选择的是 outside 状态，则后续只能选择 outside 的格子
        if (this.firstSelectedStatus === false && item.device.is_locked) {
          return true
        }
        // 如果第一个选择的是 inside 状态，则后续只能选择 inside 的格子
        if (this.firstSelectedStatus === true && item.device.is_locked === false) {
          return true
        }
        return false
      }
    },

    // 切换选中状态
    toggleSelection(row) {
      console.log('row', row)
      // // 选中的讲解器列表
      // !this.device_numbers.includes(row.device.device_number) &&
      //   this.device_numbers.push(row.device.device_number)
      // console.log(this.device_numbers)

      // 更新选中状态
      const newIsSelected = !this.list.find((item) => item.position === row.position).isSelected

      // 根据选中状态更新 device_numbers
      if (newIsSelected) {
        // 选中时添加设备编号
        !this.device_numbers.includes(row.device.device_number) &&
          this.device_numbers.push(row.device.device_number)
      } else {
        // 取消选中时移除设备编号
        this.device_numbers = this.device_numbers.filter(
          (number) => number !== row.device.device_number
        )
      }
      console.log('this.device_numbers', this.device_numbers)

      // 更新列表选中状态
      this.list = this.list.map((item) => {
        if (item.position === row.position) {
          return { ...item, isSelected: newIsSelected }
        }
        return item
      })

      if (this.firstClickStatus === null) {
        // 第一次点击，记录点击的 is_locked 状态
        this.firstClickStatus = row.device.is_locked

        console.log('firstClickStatus', this.firstClickStatus)
        this.list = this.list.map((item) => {
          return {
            ...item,
            disabled:
              (item.device?.is_locked === false && row.device.is_locked === true) ||
              (item.device?.is_locked === true && row.device.is_locked === false) ||
              !item.device
          }
        })
      }

      if (this.device_numbers.length === 0) {
        this.firstClickStatus = null
        this.list = this.list.map((item) => ({
          ...item,
          disabled: !item.device
        }))
      }

      // if (!this.clickStatus[row.position]) {
      //   this.$message({
      //     message: `Item ${row.position} 第一次被点击`,
      //     type: 'success'
      //   })
      //   this.$set(this.clickStatus, row.position, true) // 标记为已点击
      // } else {
      //   this.$message({
      //     message: `Item ${row.position} 已经被点击过`,
      //     type: 'warning'
      //   })
      // }
    },

    // 锁定/锁定
    submit(lock_status) {
      deviceLock({
        cabinet_id: this.cabinet_id,
        device_numbers: this.device_numbers,
        lock_status
      }).then(() => {
        this.$parent.fetchData()
        this.dialogFormVisible = false
        this.$parent.$notify({
          title: '成功',
          message: `${lock_status === 'lock' ? '锁定' : '解锁'}成功`,
          type: 'success'
        })
        this.firstSelectedStatus = null // 重置选择状态
        this.firstClickStatus = null
      })
    },

    // 取消操作
    cancel() {
      this.list.forEach((row) => {
        console.log('row', row)
        row.isSelected = false // 清除所有选中状态
      })
      this.firstSelectedStatus = null // 重置选择状态
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.grid-container {
  display: flex;
  // flex-direction: column;
  align-items: center; /* 居中对齐 */
  padding: 0 20px;
  flex-wrap: wrap;
  justify-content: space-between;
}

.el-checkbox {
  margin: 5px;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  border-radius: 6px; /* 圆角 */
  font-size: 16px;
  font-weight: bold;
}

/* 隐藏 el-checkbox 默认的选框 */
.el-checkbox .el-checkbox__input {
  position: relative;
  margin-right: 0; /* 隐藏选择框与文字之间的默认间距 */
}

.el-checkbox .el-checkbox__input .el-checkbox__inner {
  visibility: hidden; /* 隐藏默认选择框 */
  border: none; /* 移除默认边框 */
}

.el-checkbox .el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: transparent !important; /* 隐藏选中时的背景色 */
  border-color: transparent !important; /* 隐藏选中时的边框色 */
}

/* 自定义checkbox的边框，只保留外边框 */
.el-checkbox {
  display: inline-block;
  position: relative;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  border: 2px solid #d0e2ff; /* 默认未选中状态的边框 */
  border-radius: 6px;
  font-size: 16px;
  font-weight: bold;
}

.selected {
  background-color: #cfe4ff !important; /* 选中时背景色 */
  border: 1px solid #409eff !important; /* 选中时边框 */
  color: #409eff !important; /* 选中时文字颜色 */
}

.dialog-footer {
  text-align: right;
}

.el-button {
  margin-left: 10px;
}

.el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

.el-button--primary:hover,
.el-button--danger:hover {
  opacity: 0.8;
}

.grid-item {
  width: 80px;
  height: 40px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 14px;
  cursor: pointer;
  position: relative;
  /* 机柜内 */
  &.inside {
    background-color: #dbe4fb;
    border: 1px solid #dbe4fb;
    color: #666;
    &.active {
      background-color: #fff;
      border: 1px solid #1677ff;
      color: #1677ff;
    }
  }

  /* 机柜外 */
  &.outside {
    background-color: #ffd5c2;
    border: 0;
    color: #dc4a4a;
    &.active {
      background-color: #fff;
      border: 0;
    }
  }
  &.is-disabled {
    background-color: #ebebeb;
    color: #b6b6b6;
    border: 0;
    &.inside {
      background-color: #dbe4fb;
      border: 0;
      color: #666;
    }
    &.outside {
      background-color: #ffd5c2;
      border: 0;
      color: #dc4a4a;
    }
  }
  .upgrade_status {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 20px;
    height: 20px;
    overflow: hidden;

    &::before {
      content: '';
      position: absolute;
      bottom: -1px;
      right: -1px;
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 0 0 20px 20px;
      border-bottom-right-radius: 6px; /* 添加右下角圆角 */
    }
    &.upgrade_status_0::before {
      border-color: transparent transparent #d8d8d8 transparent; /* 灰色三角形 */
    }
    &.upgrade_status_1::before {
      border-color: transparent transparent #71c78c transparent; /* 绿色三角形 */
    }

    p {
      position: absolute;
      bottom: 1px;
      right: 3px;
      margin: 0;
      font-size: 10px;
      color: #fff;
      z-index: 1;
    }
  }
  .upgrade_progress {
    width: 60px;
    height: 14px;
    background-image: linear-gradient(270deg, #6caaff00 0%, #3a71ff 95%);
    border-radius: 6rpx 0 11rpx 0;
    position: absolute;
    top: 0px;
    left: 0px;
    border-top-left-radius: 6px;
    color: #fff;
    text-align: center;
    font-size: 12px;
  }
}

.explain {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  & > p {
    color: #666;
    position: relative;
    width: 150px;
    &::after {
      content: '';
      width: 80px;
      height: 24px;

      position: absolute;
      top: 50%;
      left: 50px;
      transform: translateY(-50%);
    }
    &.inside::after {
      background-color: #dbe4fb;
    }
    &.outside::after {
      background-color: #ffd5c2;
    }
  }
}
</style>
